package duyd.transaction.training.service.annotation;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import duyd.transaction.training.domain.Contact;
import duyd.transaction.training.repository.ContactRepository;
import duyd.transaction.training.service.ContactService;

@Service("contactService")
@Repository
@Transactional
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactRepository contactRepository;

    @Transactional(readOnly = true)
    public List<Contact> findAll() {
        return Lists.newArrayList(contactRepository.findAll());
    }

    @Transactional(readOnly = true)
    public Contact findOne(Long id) {
        return contactRepository.findOne(id);
    }

    public Contact save(Contact contact) {
        return contactRepository.save(contact);
    }

    @Transactional(propagation = Propagation.NEVER)
    public long countAll() {
        return contactRepository.countAll();
    }

    public List<Contact> findByFirstNameAndLastName(String firstName, String lastName) {
        return Lists.newArrayList(contactRepository.findByFirstNameAndLastName(firstName, lastName));
    }

    public List<Contact> findByBirthDate(Date birthDate) {
        return Lists.newArrayList(contactRepository.findByBirthDate(birthDate));
    }

}
